package com.school.movie.business.web.controller;

import com.school.movie.business.web.model.vo.ActorVo;
import com.school.movie.common.utils.BaseConstants;
import com.school.movie.common.bean.PageBean;
import com.school.movie.service.imp.ActorServiceImp;
import com.school.movie.service.imp.SysLogServiceImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @author zhaoren
 */
@Controller
@RequestMapping("/actor")
@Slf4j
@Order(1)
public class ActorController {

    @Autowired
    ActorServiceImp actorService;

    @Autowired
    SysLogServiceImp logService;

    @Value("${upload.absPath}")
    String absPath;

    /**
     * @param pager
     * @param session
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public String list(PageBean pager, HttpSession session, ModelMap map) {
        int cnt = actorService.count();
        pager.setTotalRows(cnt);
        List<ActorVo> actorList = actorService.list(pager);
        pager.setData(actorList);
        map.addAttribute("pager", pager);
        return "actor";
    }

    /**
     * @param ids
     * @param pager
     * @param map
     * @return
     */
    @RequestMapping("/del")
    public String del(String ids, PageBean pager, ModelMap map) {
        String arr[] = ids.split(",");
        actorService.del(arr);
        logService.add(BaseConstants.LOG_DEL, "删除了编号为：" + ids + "的演员");
        int cnt = actorService.count();
        pager.setTotalRows(cnt);
        List<ActorVo> actorList = actorService.list(pager);
        pager.setData(actorList);
        map.addAttribute("pager", pager);
        return "actor";
    }

    /**
     * @param actorId
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public String update(int actorId, ModelMap map) {
        ActorVo actor = actorService.getById(actorId);
        map.addAttribute("actor", actor);
        return "actorAdd";
    }

    @RequestMapping("/init")
    public String init(ModelMap map) {
        return "actorAdd";
    }

    /**
     * 上传文件
     *
     * @param request
     * @param session
     * @param file1
     * @param actor
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/save")
    public String save(HttpServletRequest request, HttpSession session, MultipartFile file1, ActorVo actor, ModelMap map) throws Exception {
        if (actor.getActorId() > 0) {
            if (file1.getOriginalFilename().equals("")) {
                ActorVo a = actorService.getById(actor.getActorId());
                actor.setCimg(a.getCimg());
            } else {
                String picName = UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                actor.setCimg("card/" + newFileName);
            }
            actorService.update(actor);
            logService.add(BaseConstants.LOG_UPDATE, actor.toString());
        } else {
            if (file1.getOriginalFilename().equals("")) {
                actor.setCimg("");
            } else {
                String picName = "A" + UUID.randomUUID().toString();
                // 截取文件的扩展名(如.jpg)
                String oriName = file1.getOriginalFilename();
                System.out.println("--上传文件名-->>" + oriName);
                String extName = oriName.substring(oriName.lastIndexOf("."));

                String newFileName = picName + extName;
                File targetFile = new File(absPath, newFileName);
                // 保存文件
                file1.transferTo(targetFile);
                actor.setCimg("card/" + newFileName);
            }
            actorService.add(actor);
            logService.add(BaseConstants.LOG_ADD, actor.toString());
        }
        int cnt = actorService.count();
        PageBean pager = new PageBean();
        pager.setTotalRows(cnt);
        List<ActorVo> actorList = actorService.list(pager);
        pager.setData(actorList);
        map.addAttribute("pager", pager);
        return "actor";
    }

}
